% same as plotComponentErsp but with selection of electrodes, in stead of
% components
% clear;
% clc;

EEG = pop_loadset('filepath','D:\Marije\ICA Analyze\CW Sphere unambiguous Response epochs low freq band passed.set');
% EEG = pop_loadset('filepath','D:\Marije\ICA Analyze\RV Sphere unambiguous Response epochs alpha band passed.set'); 
% EEG = pop_loadset('filepath','D:\Marije\ICA Analyze\CW FaceHouse Unambiguous House Stimulus alpha.set'); 

% load('CW_Chanlocs.mat');

labels = {EEG.chanlocs.labels};

subjects;
% regions = {'PAR-O','OCC-V','OCC-D','IPS-S', 'PAR-A','IPS-I','TEM-S','TEM-M','TEM-I' }; %,,
regions = {'OCC-V'}; %,,
% 
for i = 1:length(regions);
    region = regions{i};
    Id = strcmp({subject.CW.Electrodes.Region},region);
     tempId = find(Id);
    for j = 1:length(tempId);
        if subject.CW.Electrodes(tempId(j)).RejectInPreprocessing == 1;
        Id(tempId(j))= 0;
    end;
    end;
    Id = find(Id);
    elecs{i} = {subject.CW.Electrodes(Id).Label};
end
elecSelection = [elecs{1}]; %,elecs{2},elecs{3},elecs{4},elecs{5},elecs{6},elecs{7},elecs{8},elecs{9}
 
% electrodes in selected components CW
% elecSelection = {'T01','T02','T03','T04','T05','T06','T07','T09','T10','T11','T12','T13','T14','T19','T21','T22','OcO2','OcO3','OcO4','OcO6','OcO9','Oc10','Oc11','Oc12','Oc13','sTa2','sTa3','sTa4','P01','P02','P04','P05','P06','P07','P08','P09','P10','P11','P12','P13','P14','P15'};
% par_elec = {'T01','T02','T03','T04','T05','T06','T07','T09','T10','T11','T12','T13','T14','T19','T21','T22','OcO6','P01', 'P02','P04','P05','P06','P07','P08', 'P09','P10','P11','P12','P13','P14','P15'};
% occ_elec = {'OcO2','OcO3','OcO4', 'OcO9','Oc10','Oc11','Oc12','Oc13','sTa2','sTa3','sTa4'};
% para_elec = {'T07','P04','P10'}; % Frontal or temperal niet meegenomen: 'T19','T21','T22','sTa4','P01','P02','P09',
% paro_elec = {'T09','OcO6'};
% ipss_elec = {'P11','P12','P13'};
% ipsi_elec = {'T01','T02','T03','T04','T05','T06','T10','T11','T12','T13','T14','P06','P07','P08','P14','P15'};
% occv_elec = {'Oc12','Oc13','sTa2','sTa3'};
% occd_elec = {'OcO2','OcO3','OcO4','OcO9','Oc10','Oc11'};
% elecs = {paro_elec,occv_elec,occd_elec, ipss_elec,para_elec,ipsi_elec};
% elecSelection = {'T07','P04','P10','T09','OcO6','P05','P11','P12','P13','T01','T02','T03','T04','T05','T06','T10','T11','T12','T13','T14','P06','P07','P08','P14','P15','Oc12','Oc13','sTa2','sTa3','OcO2','OcO3','OcO4','OcO9','Oc10','Oc11'};

esIdx = [];
 for i = 1:length(elecSelection)
    esIdx(i) = find(strcmp(elecSelection{i},labels));
end;

% In subplots
figure; ersp_output = [];
for i = 1:length(esIdx);
    h = subplot(10,10,i);
ersp_output(:,:,i) = pop_newtimef(EEG, 1, esIdx(i), [-4000 4000],0,'plotitc','off', 'freqs',[4,30],'baseline', [-4000 -2000; 2000 4000]);
title (['Electrode' elecSelection(i)])
end

for i = 1:length(regions);
    para_elec = elecs{i};
for a = 1:length(para_elec)
paraIde(a) = find(strcmp(elecSelection,para_elec{a}));
end;
ersp_para = NaN(length(ersp_output(:,1,1)),length(ersp_output(1,:,1)),length(para_elec));
for j = 1:length(paraIde);
ersp_para(:,:,j) = ersp_output(:,:,paraIde(j));
end
% av_para = mean(ersp_para,3);
% fig = figure; imagesc(av_para, [-2.5 2.5]);
% title (['CW - Sphere Ambiguous Response - selected on location - ' regions{i}]);
% colorbar
% set(gca,'xTick',1:33.3:200); set(gca,'xTickLabel',[-3000, -2000, -1000, 0, 1000, 2000,3000]);
% set(gca,'YDir','normal');
% % % set(gca,'yTick',1:40:161); set(gca,'yTickLabel',[30,40,50,60,70]); 
% % set(gca,'yTick',1:4:25); set(gca,'yTickLabel',[7,8,9,10,11,12,13]);
% % set(gca,'yTick',1:4:69); set(gca,'yTickLabel',[13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]);
% freq = [4:1:30];
% set(gca,'yTick',1:4:105); set(gca,'yTickLabel',freq); 
% % set(gca,'yTick',1:4:13); set(gca,'yTickLabel',[4,5,6,7]); 
% saveFigure(fig, ['C:\Users\Marije\Documents\ECoG\ICA\Low freq\'], ['CW - SphereAmbiguousResponse - selected on location - averaged ersp - ' regions{i}]);
% close(fig);
% clearvars para_elec paraIde ersp_para av_para;
av_para2 = mean(ersp_para,3);
clearvars -except av_para1 av_para2;
end;


